package com.zjhcsoft.auth.service.impl;

import com.zjhcsoft.auth.entity.AppResource;
import com.zjhcsoft.auth.dao.AppResourceMapper;
import com.zjhcsoft.auth.entity.Channel;
import com.zjhcsoft.auth.entity.Corporation;
import com.zjhcsoft.auth.service.AppResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjhcsoft.auth.service.ChannelService;
import com.zjhcsoft.auth.service.CorporationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务系统信息表 服务实现类
 * </p>
 *
 * @author wzya
 * @since 2021-03-29
 */
@Service
public class AppResourceServiceImpl extends ServiceImpl<AppResourceMapper, AppResource> implements AppResourceService {

    @Resource
    ChannelService channelService;
    @Resource
    CorporationService corporationService;


    @Override
    public List<AppResource> getAppResourceByChannelCodesAndCorporationCode(List<String> channelCodes, String corporationCode) {
        List<Long> channelIds = channelService.lambdaQuery().in(Channel::getChannelCode,channelCodes).list().stream().map(channel -> channel.getId()).collect(Collectors.toList());
        Long corporationId = corporationService.lambdaQuery().eq(Corporation::getCorporationCode,corporationCode).one().getId();
        return this.lambdaQuery().in(AppResource::getChannelId,channelIds).eq(AppResource::getCorporationId,corporationId).list();
    }

    @Override
    public AppResource getAppResourceByChannelCodeAndCorporationCode(String channelCode, String corporationCode) {

        Long channelId = channelService.lambdaQuery().eq(Channel::getChannelCode,channelCode).one().getId();
        Long corporationId = corporationService.lambdaQuery().eq(Corporation::getCorporationCode,corporationCode).one().getId();
        return this.lambdaQuery().eq(AppResource::getChannelId,channelId).eq(AppResource::getCorporationId,corporationId).one();
    }
}
